package com.hmall.common.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.UUID;

@Slf4j
@RequiredArgsConstructor
public class RabbitMqHelper {

    private final RabbitTemplate rabbitTemplate;

    public void sendMessage(String exchange, String routingKey, Object msg){
      log.debug("准备发送信息，exchange:{},routingKey:{},msg:{}",exchange,routingKey,msg);
      rabbitTemplate.convertAndSend(exchange,routingKey,msg);
    }

    public void sendDelayMessage(String exchange, String routingKey, Object msg, int delay){
        log.debug("准备发送信息，exchange:{},routingKey:{},msg:{},delay:{}",exchange,routingKey,msg,delay);
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,message ->{
            message.getMessageProperties().setDelay(delay);
            return message;
        });
    }

    public void sendMessageWithConfirm(String exchange, String routingKey, Object msg, int maxRetries){
        log.debug("准备发送信息，exchange:{},routingKey:{},msg:{},maxRetries:{}",exchange,routingKey,msg,maxRetries);
        CorrelationData cd=new CorrelationData(UUID.randomUUID().toString());
        cd.getFuture().addCallback(new ListenableFutureCallback<CorrelationData.Confirm>() {
            int retryCount;
            @Override
            public void onFailure(Throwable ex) {
                log.error("处理ack回执失败"+ex);
            }

            @Override
            public void onSuccess(CorrelationData.Confirm result) {
                if(result!=null&&!result.isAck()){
                    log.debug("消息发送失败，收到nack，已重试次数：{}",retryCount);
                    if(retryCount>=maxRetries){
                        log.error("消息发送重试次数耗尽，发送失败");
                        return;
                    }
                    CorrelationData cd=new CorrelationData(UUID.randomUUID().toString());
                    cd.getFuture().addCallback(this);
                    rabbitTemplate.convertAndSend(exchange,routingKey,msg,cd);
                    retryCount++;
                }
            }
        });
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,cd);
    }
}